package com.frame.management;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.excel.Utils;


public class Login {
	private JFrame f = new JFrame("登录");
	private JTextField text1 = new JTextField(10);
	private JPasswordField text2 = new JPasswordField(10);
	private JLabel infor = new JLabel();
	private JButton bt1 = new JButton("登录");
	private JButton bt2 = new JButton("重置");
	private JRadioButton rb1 = new JRadioButton("管理员");
    private JRadioButton rb2 = new JRadioButton("辅导员");
    private ButtonGroup rb3=new ButtonGroup();
    public String value;
	
	public void view() {
		f.setLayout(new BorderLayout(3, 4));
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		f.add(p1, BorderLayout.NORTH);
		f.add(p2, BorderLayout.CENTER);
		f.add(p3, BorderLayout.SOUTH);
		

		// 顶部
		JLabel l1 = new JLabel("用户登录");
		l1.setLayout(new FlowLayout(FlowLayout.CENTER));
		p1.add(l1);

		// 中间部分
		p2.setLayout(new GridLayout(4, 2));
		JPanel p4 = new JPanel();
		JPanel p5 = new JPanel();
		JPanel p6 = new JPanel();
		JPanel p7= new JPanel();
		
		p2.add(p4);
		p2.add(p5);
		p2.add(p6);
		p2.add(p7);
		

		p4.setLayout(new FlowLayout(FlowLayout.CENTER));
		p5.setLayout(new FlowLayout(FlowLayout.CENTER));
		p6.setLayout(new FlowLayout(FlowLayout.CENTER));
		p7.setLayout(new FlowLayout(FlowLayout.CENTER));
	

		JLabel username = new JLabel("用户名：");
		p4.add(username);
		p4.add(text1);

		JLabel password = new JLabel("密    码：");
		p5.add(password);
		p5.add(text2);

		p6.add(rb1);
		p6.add(rb2);
		
		p7.add(infor);

		// 底部
		p3.setLayout(new FlowLayout(FlowLayout.CENTER));
		p3.add(bt1);
		p3.add(bt2);

		rb3.add(rb1);
		rb3.add(rb2);
		
		
		
		// 登录
		bt1.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				
				Enumeration<AbstractButton> en=rb3.getElements();
				while(en.hasMoreElements()){
					AbstractButton ab=en.nextElement();
					if(ab.isSelected()){
						value=ab.getText();
						if(rb1.getText().equals(value)){
							MainSystem mainSystem = new MainSystem();
							mainSystem.setVisible(true);
							//关闭窗口的事件，保存学生信息
							mainSystem.addWindowListener(new WindowAdapter() {
								@Override
								public void windowClosing(WindowEvent e) {
									Utils.createStudentExcel();
									System.out.println("关闭窗口，学生信息保存到Excel。");
									super.windowClosing(e);
								}
							});
							f.dispose();//登录成功学生关闭登录窗口
						}else if("辅导员".equals(value)){
							MainSystem2 mainSystem = new MainSystem2();
							mainSystem.setVisible(true);
							//关闭窗口的事件，保存学生信息
							mainSystem.addWindowListener(new WindowAdapter() {
								@Override
								public void windowClosing(WindowEvent e) {
									Utils.createStudentExcel();
									System.out.println("关闭窗口，学生信息保存到Excel。");
									super.windowClosing(e);
								}
							});
							f.dispose();//登录成功学生关闭登录窗口
						}
						
					}else if(!ab.isSelected()){							
						infor.setText("请选择是'管理员'还是'辅导员'");
						text1.setText(null);
						text2.setText(null);
					}
				}
				
				
			}

		});

		// 重置
		bt2.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				text1.setText(null);
				text2.setText(null);
			}
		});
		
		
		f.setSize(450, 250);
		f.setLocation(500, 170);
		f.setVisible(true);
	}


	public static void main(String[] args) {
		Login login=new Login();
		login.view();
	}

}
